package com.jin.crud.common.exception;

import com.jin.crud.common.utils.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author yuxuejin
 * @Date 2021/11/13 16:25
 * @Description: 异常处理类
 */
@ControllerAdvice
public class ResultExceptionHandler {

    /**
     * <pre>
     * ExceptionHandler相当于controller的@RequestMapping
     * 如果抛出的的是ServiceException，则调用该方法
     * </pre>
     *
     * @param re 业务异常
     *
     */
    @ExceptionHandler(ResultException.class)
    @ResponseBody
    public ResultUtil<Object> handle(ResultException re) {
        ResultUtil<Object> res = new ResultUtil<>();
        res.error(re.getCode(), re.getMessage());
        return res;
    }

}
